package com.atguigu.gmall.order.config;

import com.rabbitmq.client.AMQP;
import org.springframework.amqp.core.*;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;


/**
 * @author: Mamba
 * @version: 1.0
 * @Time: 2021/1/13 20:24
 * @Description:    订单 取消相关
 * @Au  java配置类
 *
 * Configuration中包含Component
 */
@Configuration
public class DeadLetterMQConfig {
/**
 * 取消订单，发送延迟队列
 */
    public static final String EXCHANGE_DIRECT_ORDER_CANCEL = "exchange.direct.order.cancel";
    public static final String ROUTING_ORDER_CANCEL1 = "order.create1";
    public static final String ROUTING_ORDER_CANCEL2 = "order.create2";
    //延迟取消订单队列
    public static final String QUEUE_ORDER_CANCEL1 = "queue.order.cancel1";
    public static final String QUEUE_ORDER_CANCEL2 = "queue.order.cancel2";
    //取消订单 延迟时间 单位：毫秒
    public static final int DELAY_TIME = 2*60*60*1000;//全局设置2小时
    //TTL的队列
    @Bean
    public Queue queueOrderCancel1(){
        Map<String,Object> arguments = new HashMap<>();
        arguments.put("x-dead-letter-exchange", EXCHANGE_DIRECT_ORDER_CANCEL);
        arguments.put("x-dead-letter-routing-key",ROUTING_ORDER_CANCEL2);
        arguments.put("x-message-ttl",DELAY_TIME);//单位是毫秒
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL1).withArguments(arguments).build();
    }
    //普通队列
    @Bean
    public Queue queueOrderCancel2(){
        return QueueBuilder.durable(QUEUE_ORDER_CANCEL2).build();
    }
    //交换机（普通）
    @Bean
    public Exchange exchangeOrderCancel(){
        return ExchangeBuilder.directExchange(EXCHANGE_DIRECT_ORDER_CANCEL).build();
    }
    //绑定2遍
    @Bean
    public Binding bindingOrderCancel1(@Qualifier(value = "queueOrderCancel1") Queue qc1,
                                       @Qualifier(value = "exchangeOrderCancel")Exchange eoc){
        return BindingBuilder.bind(qc1).to(eoc).with(ROUTING_ORDER_CANCEL1).noargs();
    }
    @Bean
    public Binding bindingOrderCancel2(@Qualifier(value = "queueOrderCancel2") Queue qc2,
                                       @Qualifier(value = "exchangeOrderCancel") Exchange eoc){
        return BindingBuilder.bind(qc2).to(eoc).with(ROUTING_ORDER_CANCEL2).noargs();
    }
}
